<template>
  <div class="orderDetail app-container">
    <div class="orderDetail_box">
      <div style="font-size: 16px;margin-bottom: 20px;"
        class="big_title_style">
        订单详情
      </div>
      <el-form label-width="96px"
        size="mini">
        <div class="show_detail_info">
          <el-row>
            <el-col :span="8">
              <el-form-item label="订单状态：">{{dtInfo.payStatusName}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单编号：">{{dtInfo.orderNo}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="创建时间：">{{dtInfo.createTime}}</el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="开单员工：">{{dtInfo.createName}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开单门店：">{{dtInfo.storeName || ''}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收款人：">{{dtInfo.skPeople || ''}}</el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注信息：">{{dtInfo.remark || '无'}}</el-form-item>
            </el-col>
          </el-row>
        </div>

        <div style="font-size: 16px;margin-bottom: 20px;"
          class="big_title_style mt20">
          会员信息
        </div>
        <div class="show_detail_info">
          <el-row>
            <el-col :span="8">
              <el-form-item label="客户姓名：">{{dtInfo.customerName}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="客户手机号：">{{dtInfo.phone}}</el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="客户等级：">{{dtInfo.levelName}}</el-form-item>
            </el-col>
          </el-row>
        </div>

        <div style="font-size: 16px;margin-bottom: 20px;"
          class="big_title_style mt20">
          消费信息
        </div>
        <el-table :data="dtInfo.list"
          border>
          <el-table-column label="类型"
            prop="orderTypeStr"
            align="center"></el-table-column>
          <el-table-column label="项目名称"
            prop="proName"
            align="center"></el-table-column>
          <el-table-column label="sku"
            prop="skuDesc"
            align="center"></el-table-column>
          <el-table-column label="价格"
            prop="itemAmountStr"
            align="center"></el-table-column>
          <el-table-column label="数量"
            prop="itemNumber"
            align="center"></el-table-column>
          <el-table-column label="会员折扣"
            prop="discountAmountStr"
            align="center"></el-table-column>
          <el-table-column label="现金支付"
                prop="payAmountStr"
                align="center"></el-table-column>
          <el-table-column label="金币支付"
              prop="payByBalanceStr"
              align="center"></el-table-column>
          <el-table-column label="银币支付"
              prop="payByRedBagStr"
              align="center"></el-table-column>
          <el-table-column label="小计"
            align="center">
            <template slot-scope="scope">
              <span>{{(scope.row.itemAmountStr)}}</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="settle_info mt20">
          <div class="settle_info_value">
            <el-form-item label="订单合计："
              class="item_settle_info">￥{{dtInfo.orderAmount}}</el-form-item>
            <el-form-item label="会员折扣："
              class="item_settle_info">-￥{{dtInfo.discountAmount}}</el-form-item>
            <el-form-item label="应收款："
              class="item_settle_info">￥{{dtInfo.payAmount}}</el-form-item>
            <el-form-item label="折后金额："
                class="item_settle_info">￥{{dtInfo.reGongMoney}}</el-form-item>
            <el-form-item label="现金付款："
              class="item_settle_info">￥{{dtInfo.payStatus == '1'? dtInfo.payAmount: 0}}</el-form-item>
            <el-form-item label="金币付款："
                class="item_settle_info">￥{{dtInfo.payStatus == '1'? dtInfo.payBalance: 0}}</el-form-item>
            <el-form-item label="银币付款："
                    class="item_settle_info">￥{{dtInfo.payStatus == '1'? dtInfo.payRedBag: 0}}</el-form-item>
            <el-form-item label="付款时间："
              class="item_settle_info">{{dtInfo.skDate || ''}}</el-form-item>
          </div>
          <div class="settle_info_opt">
            <el-button size="small"
              type="primary"
              plain
              style="width: 100px"
              @click="toGetMoneyOpt('dazhe')"
              v-if="dtInfo.payStatus == '0' && userInfo.postId != 4">打折</el-button>
            <el-button size="small"
              type="primary"
              style="width: 100px"
              @click="toGetMoneyOpt('shouk')"
              v-if="dtInfo.payStatus == '0' && userInfo.postId != 4">收款</el-button>
            <el-button size="small"
              style="width: 100px"
              @click="toBack">返回</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <el-dialog :title="payType == 'dazhe'? '设置折扣': '确认收款'"
      :visible.sync="dialogVisible"
      width="560px"
      :before-close="handleClose">
      <div class="affirm_info_box">
        <div class="show_customer_info">
          <div class="show_customer_header"> <img :src="dtInfo.customerImage"
              alt=""> </div>
          <div class="show_customer_info_box">
            <div class="show_customer_info_top">
              <div class="show_customer_name">{{dtInfo.customerName}}</div>
              <el-tag type="danger"
                class="ml10"
                size="mini">{{dtInfo.levelName || '--'}}</el-tag>
            </div>
            <div class="show_customer_phone">{{dtInfo.phone}}</div>
          </div>
        </div>
        <div class="collection_info">
          <div class="collection_info_item">
            <div class="collection_info_item_label">应收款</div>
            <div class="collection_info_item_num">￥{{dtInfo.payAmount}}</div>
          </div>
          <div class="collection_info_item"
            style="flex: 2">
            <div class="collection_info_item_label">支付状态</div>
            <div class="collection_info_item_stauts">
              <span class="collection_info_item_stauts_txt">{{dtInfo.payStatusName}}</span>
              <div class="collection_info_item_stauts_refresh"
                @click="toRefreshPayStatus">刷新支付状态</div>
            </div>
          </div>
        </div>
        <!-- <div class="select_collection_type">
          <div class="select_collection_type_label">请选择收款方式</div>
          <div class="select_collection_type_box">
            <div class="collection_type_item"
              v-for="(item, idx) in payMethods"
              :key="idx"
              @click="toSelectPayMethod(idx)">
              <div class="collection_card_item_select"
                v-if="item.isSelect">
                <i class="el-icon-check"></i>
              </div>

              <div class="collection_type_item_icon"> <img src=""
                  alt=""> </div>
              <div class="collection_type_item_label">现金</div>
            </div>
          </div>
        </div> -->
        <el-alert class="select_collection_Notice"
          title="请确认已经收到的款项信息，确认后将无法撤回"
          type="warning"
          show-icon
          :closable="false">
        </el-alert>
        <el-form v-if="payType != 'dazhe'">
          <el-form-item label="优惠："
            label-width="88px">
            <el-checkbox v-model="isDiscount">折扣订单</el-checkbox>
          </el-form-item>
        </el-form>
        <el-form v-if="payType != 'dazhe'">
          <el-form-item label="收款方式："
            label-width="88px">
            <el-select v-model="payMethod"
              placeholder="请选择收款方式"
              clearable
              style="width: 100%">
              <el-option v-for="dict in payMethods"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-form>
        <el-form>
          <el-form-item :label="payType == 'dazhe'? '折后金额：': '实收：'"
            label-width="88px">
            <el-input placeholder="请输入顾客实付金额"
              v-model.trim="realMoneyNum"
              @input="realMoneyNum = onInput(realMoneyNum)">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-form>
        <el-form>
          <el-form-item label="备注："
            label-width="88px">
            <el-input type="textarea"
              v-model.trim="remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer"
        class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button @click="toGetMoney"
          type="primary">{{payType == 'dazhe'? '保存': '收款'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  orderDetailPc,
  getMoney,
  dazheMoney
} from "@/api/service/makePlan";
import { onInput } from "@/utils/util";
import { mapGetters } from 'vuex'

export default {
  data () {
    return {
      dialogVisible: false,
      dtId: '',
      dtInfo: {},
      realMoneyNum: '',
      payMethod: '',
      isDiscount: false,
      remark: '',
      payMethods: [],
      payType: 'shouk'
    }
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  created () {
    // this.dtId = this.$route.query.detailId
    // this.getData()

    // this.getDicts("pay_method").then(res => {
    //   let data = res.data
    //   data.forEach(item => {
    //     item.isSelect = false
    //   })
    //   this.payMethods = data;
    // });
  },
  activated () {
    this.getDicts("order_pay_method").then(response => {
      let data = response.data
      data = data.filter(v => v.dictValue != 'wechat')
      this.payMethods = data;
    });

    this.resetForm()
    
    this.dtId = this.$route.query.detailId
    this.getData()
  },
  methods: {
    onInput,
    // toSelectPayMethod (idx) {
    //   this.payMethods.forEach((item, index) => {
    //     if (index == idx) {
    //       item.isSelect = true
    //     } else {
    //       item.isSelect = false
    //     }
    //   })
    // },
    toRefreshPayStatus () {
      this.getData()
    },
    async getData () {
      let res = await orderDetailPc(this.dtId)
      if (res) {
        this.dtInfo = res.data
      }
    },
    resetForm () {
      this.realMoneyNum = ''
      this.remark = ''
      this.payMethod = ''
      this.isDiscount = false
    },
    toGetMoneyOpt (type) {
      this.resetForm()
      this.payType = type
      this.dialogVisible = true
    },
    toBack () {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1)
      // this.$router.push({
      //   path: "/store/workbench/medicine",
      //   query: {
      //     t: Date.now()
      //   }
      // });
    },
    handleClose () {
      this.dialogVisible = false
      this.payType = ''
    },
    toGetMoney () {
      this.$confirm(this.payType == 'dazhe'? '确认打折?':'确认收款?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.payType == 'dazhe') {
          dazheMoney({
            orderUid: this.dtId,
            payAmount: this.realMoneyNum * 100,
            remark: this.remark,
            // discount: this.isDiscount ? '1' : '0',
            // payMethod: this.payMethod
          }).then(res => {
            this.msgSuccess('收款成功')
            this.handleClose()
            this.getData()
          })
        } else {
          getMoney({
            orderUid: this.dtId,
            payAmount: this.realMoneyNum * 100,
            remark: this.remark,
            discount: this.isDiscount ? '1' : '0',
            payMethod: this.payMethod
          }).then(res => {
            this.msgSuccess('收款成功')
            this.handleClose()
            this.getData()
          })
        }

      }).catch(() => {

      });
      // this.$router.push({
      //   path: '/store/workbench/serviceOpt'
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  display: block;
  width: 100%;
  height: 100%;
}
.orderDetail_box {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
}
.show_detail_info {
  font-size: 14px;
  color: #333;
}

.settle_info {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.item_settle_info {
  font-size: 14px;
  color: #333;
  &:not(:last-child) {
    margin-bottom: 16px;
  }
}

.affirm_info_box {
  .show_customer_info {
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 4px;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .show_customer_header {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      margin-right: 10px;
      background-color: #fff;
    }
    .show_customer_info_box {
      flex: 1;
      overflow: hidden;
    }
    .show_customer_info_top {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      .show_customer_name {
        font-size: 12px;
        color: #333;
      }
    }
    .show_customer_phone {
      font-size: 12px;
      color: #333;
    }
  }
  .collection_info {
    display: flex;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
    margin-bottom: 15px;
    .collection_info_item {
      flex: 1;
      .collection_info_item_label {
        font-size: 14px;
        color: #999;
        line-height: 1;
        margin-bottom: 10px;
      }
      .collection_info_item_num {
        font-size: 18px;
        color: #333;
        font-weight: 600;
      }
      .collection_info_item_stauts {
        display: flex;
        align-items: center;
        .collection_info_item_stauts_txt {
          font-size: 16px;
          color: #c03639;
          font-weight: 800;
        }
        .collection_info_item_stauts_refresh {
          cursor: pointer;
          padding: 6px 10px;
          border-radius: 4px;
          background-color: #999;
          color: #fff;
          font-size: 12px;
          line-height: 1;
          margin-left: 10px;
        }
      }
    }
  }
  .select_collection_type {
    margin-bottom: 15px;
    .select_collection_type_label {
      font-size: 14px;
      color: #999;
      line-height: 1;
    }
    .select_collection_type_box {
      display: flex;
      flex-wrap: wrap;
      .collection_type_item {
        cursor: pointer;
        margin-top: 10px;
        width: 160px;
        height: 64px;
        border: 1px solid #eee;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        &:not(:nth-child(3n)) {
          margin-right: 20px;
        }

        .collection_card_item_select {
          position: absolute;
          top: 0;
          right: 0;
          height: 16px;
          width: 16px;
          border-radius: 4px;
          background-color: #5da9ef;
          font-size: 12px;
          color: #fff;
          text-align: center;
          line-height: 16px;
        }
        .collection_type_item_icon {
          width: 32px;
          height: 32px;
          margin-right: 10px;
        }
        .collection_type_item_label {
          font-size: 15px;
          color: #333;
        }
      }
    }
  }
  .select_collection_Notice {
    margin-bottom: 15px;
  }
}
</style>
